# -*- coding: utf-8 -*-
from django.core.urlresolvers import reverse
from django.views.generic.list_detail import object_list, object_detail
from django.views.generic.create_update import create_object, delete_object, \
    update_object
#from ragendja.template import render_to_response, render_to_string

from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.template import RequestContext
from google.appengine.ext import db
from google.appengine.api import images
#from bvuserapp.models import User
from django.contrib.auth.models import User
from bvuserapp import user
import logging
import datetime
import sys

from metaweb import get_domain_used, get_bioventurist_domain, get_delimiter
from user import get_current_user, get_user_info

class temp_data:
    edit__data=''
    edit_time=''
def mt_user_page(request):
    
    if request.method == 'POST':
        files = request._get_files()
        img_file = files['img']
        user_image = img_file.read()
        request.user.user_image = db.Blob(user_image)
        request.user.put()
    
    return render_to_response('user_temp.html',{'domain_used' : get_domain_used(), 'bioventurist_domain' : get_bioventurist_domain()}, context_instance=RequestContext(request))

#def mt_user_temp_page(request):
#    return render_to_response('user_temp.html',{'domain_used' : get_domain_used(), 'bioventurist_domain' : get_bioventurist_domain()})

def simple_page(request):
    return render_to_response('simple.html',{'domain_used' : get_domain_used(), 'bioventurist_domain' : get_bioventurist_domain()}, context_instance=RequestContext(request))

def user_profile_page(request, username):
    user = User.get_by_key_name("key_"+username.lower())
    return render_to_response('user_details.html',{'domain_used' : get_domain_used(), 'bioventurist_domain' : get_bioventurist_domain(),'objuser' :user}, context_instance=RequestContext(request))


def all_user(request):
    users = User.all().order('-mt_edits')
    editlist=[]
    edit_userlist=[]
    now= datetime.datetime.now()
    for user in users:
        datalist=[]
        for f_edit in user.freebase_edit_set:
            if f_edit.timestamp != None:
                edit_delta_seconds = (now-f_edit.timestamp).seconds;
                logging.info('\nedit_delta_seconds = '+str(edit_delta_seconds)+'\n')
                datalist.append(f_edit.user)
                #print type(f_edit)
                if type(f_edit) == "bvuserapp.models.freebase_edit_link":
                    datalist.append(f_edit.topic1_name)
                    datalist.append(edit_delta_seconds)
                else:
                    datalist.append('')
                    datalist.append(edit_delta_seconds)
                if not datalist in editlist:
                    editlist.append(datalist)
                    
    editlist.sort(key=lambda x:x[2] )
    for innerlist in editlist:
##        print innerlist[0]
        edit_delta_seconds=innerlist[2]
        # see what is a reasonable unit
        if(edit_delta_seconds > 86400):
            edit_time = edit_delta_seconds / 86400
            edit_time_unit = 'days'
        elif(edit_delta_seconds > 3600):
            edit_time = edit_delta_seconds / 3600
            edit_time_unit = 'hours'
        elif(edit_delta_seconds > 60):
            edit_time = edit_delta_seconds / 60
            edit_time_unit = 'minutes'
        else:
            edit_time = edit_delta_seconds
            edit_time_unit = 'seconds'
        if innerlist[1]!='':
            liststr=str(innerlist[0])+' edited '+innerlist[1]+str(edit_time)+' '+edit_time_unit+ ' ago'
        else:
            liststr=str(innerlist[0])+' edited '+str(edit_time)+' '+edit_time_unit+ ' ago'
        edit_userlist.append(liststr)       
    return render_to_response('All_Users.html',{'domain_used' : get_domain_used(), 'bioventurist_domain' : get_bioventurist_domain(),'users' :users,'edit_userlist':edit_userlist}, context_instance=RequestContext(request))


def show_image(request):
    response = HttpResponse(request.user.user_image, mimetype="image/jpeg")
    return response

def edit_user(request):
    if request.method == 'POST':
        posts = request._get_post()
        
        request.user.first_name = posts['first_name']
        request.user.last_name = posts['last_name']
        request.user.personal_statement = posts['personal_statement']
        request.user.home_page = posts['home_page']

        request.user.put()
        
    return render_to_response('user_temp.html',{'domain_used' : get_domain_used(), 'bioventurist_domain' : get_bioventurist_domain()}, context_instance=RequestContext(request))

    
def showedited(request):
    users = User.all().order('mt_edits')
    start=datetime.datetime(2009,06,03)
    now= datetime.datetime.now()
    userlist=[]
    flag=0
    liststr=''
    logging.info('\nShowEdited: now = '+repr(now)+'\n')
    for user in users:
        if len(userlist) ==20:
            break
        for f_edit in user.freebase_edit_set:
            #f_edit.timestamp=start
            if f_edit.timestamp != None:
              
                edit_delta_seconds = (now-f_edit.timestamp).seconds;
                logging.info('\nedit_delta_seconds = '+str(edit_delta_seconds)+'\n')

                # see what is a reasonable unit
                if(edit_delta_seconds > 86400):
                    edit_time = edit_delta_seconds / 86400
                    edit_time_unit = 'days'
                elif(edit_delta_seconds > 3600):
                    edit_time = edit_delta_seconds / 3600
                    edit_time_unit = 'hours'
                elif(edit_delta_seconds > 60):
                    edit_time = edit_delta_seconds / 60
                    edit_time_unit = 'minutes'
                else:
                    edit_time = edit_delta_seconds
                    edit_time_unit = 'seconds'
                
                #print type(f_edit)
                if type(f_edit) == "bvuserapp.models.freebase_edit_link":
                    liststr=str(f_edit.user)+' edited '+f_edit.topic1_name+str(edit_time)+' '+edit_time_unit+ ' ago'
                else:
                    liststr=str(f_edit.user)+' edited '+str(edit_time)+' '+edit_time_unit+ ' ago'
                
                if not liststr in userlist:
                    userlist.append(liststr)

            if len(userlist)==20:
                break
    
    return render_to_response('edited_info.html',{'editlist':userlist})

def getprofile_img(request, username):
    user = User.get_by_key_name("key_"+username.lower())
    response = HttpResponse(user.user_image, mimetype="image/jpeg") if user.user_image else ""   
    return response
    
